Repository navigation
[Python] Make Beam the only retry layer in AnthropicModelHandler - #41359
Conversation
The Anthropic client retries failed requests twice by default, inside each of RemoteModelHandler's attempts, so a request that keeps failing was sent 18 times and the client-side throttler only saw a third of the failures. Create the client with max_retries=0, as OpenAIModelHandler does, and retry connection errors in the retry filter, which until now only the client retried.
There was a problem hiding this comment.
📋 Review Summary
This PR disables the Anthropic SDK's built-in retries (max_retries=0) in AnthropicModelHandler.create_client() and adds APIConnectionError to _retry_on_appropriate_error, ensuring Apache Beam's RemoteModelHandler and ReactiveThrottler act as the single retry and throttling layer. The implementation is clean, consistent with OpenAIModelHandler, and well-covered by unit tests.
🔍 General Feedback
- Consistent retry handling across LLM handlers: Setting
max_retries=0on theAnthropicclient and catchingAPIConnectionError(which also coversAPITimeoutError) in_retry_on_appropriate_errormatches the pattern established inOpenAIModelHandlerand prevents multiplicative retries (18 attempts instead of 6) from hiding transient failures from Beam's client-side throttler. - Follow-up consideration (
inference_argsmutation on retry): Now that even a single transient failure (APIConnectionError,429, or5xx) is retried at theRemoteModelHandler.run_inferencelevel rather than insideclient.messages.create(),AnthropicModelHandler.request()is re-invoked with the sameinference_argsdict reference on every retry. Becausemessage_from_stringandmessage_from_conversationcallinference_args.pop('max_tokens', 1024)in place withoutAnthropicModelHandler.request()copyinginference_argsfirst, a caller-suppliedmax_tokenswill be lost on retry attempts (falling back to1024). Copyinginference_argsinrequest()(asOpenAIModelHandlerdoes) would be a great follow-up fix.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @damccorm for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Mola-maker
left a comment
There was a problem hiding this comment.
I ran anthropic_inference_test.py locally: 22 passed, 2 failed. The 2 failures (test_pipeline_e2e, test_pipeline_with_system_prompt) fail identically on the base commit, they are environment failures in my sandbox (the Anthropic SDK's httpx client chokes on a proxy env var), not a regression from this PR. The PR's own new test test_retry_on_connection_error and the updated max_retries=0 assertions pass. The change itself looks right: with max_retries=0 the client no longer retries inside Beam's attempts, and APIConnectionError is now retried by Beam's filter.
|
Reminder, please take a look at this pr: @damccorm |
damccorm
left a comment
There was a problem hiding this comment.
Thanks - LGTM, but I'll wait for checks to complete before merging
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #41359 +/- ##
============================================
- Coverage 59.32% 59.21% -0.11%
Complexity 4995 4995
============================================
Files 1463 1463
Lines 186618 186621 +3
Branches 3729 3729
============================================
- Hits 110704 110514 -190
- Misses 72543 72736 +193
Partials 3371 3371
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
AnthropicModelHandlercreates the client with its defaultmax_retries=2, so the client retries inside each ofRemoteModelHandler's attempts. A request that keeps failing with 429 or 500 is sent 18 times instead of 6, and Beam's throttler only sees 6 of those failures.OpenAIModelHandleralready setsmax_retries=0for this reason.This PR:
max_retries=0;APIConnectionErrorin the retry filter. Until now only the client retried connection errors, so without this they would no longer be retried.Testing:
anthropic_inference_test.py.This continues #40914 and #40929: I've been going through how the remote LLM handlers behave when requests fail. If there's something else in this area you'd rather I work on, I'm happy to do that instead.
R: @jrmccluskey